let time = require('../../../util/time');


//增加每日视频数量记录
exports.addDailyVideoNumber = function (uid) {
    return new Promise((resolve, reject) => {
        let mysqlClient = this.app.get('mysqlClient');//获取全局mysql client
        let date = time.getTodayDate();

        let sql = "SELECT COUNT(*) as count from _daily_ad WHERE uid = ? and date = ?";
        let args = [uid, date];
        mysqlClient.query(sql, args, function (err, res) {
            if (err) return reject(err);
            let objRes = JSON.parse(JSON.stringify(res));
            //存在相同uid的数据
            if (objRes[0].count !== 0) {
                sql = "UPDATE _daily_ad SET ad = ad + 1 WHERE uid = ? and date = ?";
                args = [uid, date];
            } else {
                sql = "INSERT INTO _daily_ad (date, uid, ad) VALUES(?, ?, ?)";
                args = [date, uid, 1];
            }
            mysqlClient.query(sql, args, function (err, res) {//执行sql语句 函数insert和query等效
                if (err) return reject(err);
                resolve(res)
            });
        });
    });
}